package com.itheima.heimaai.controller;

import com.itheima.heimaai.repository.ChatHistoryRepository;
import com.itheima.heimaai.entity.vo.MessageVO;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    @Autowired
    private  ChatHistoryRepository chatHistoryRepository;

    @Autowired
    private ChatMemory chatMemory;

    /**
     * 查询会话记录列表
     * @param type
     * @return
     */
    @GetMapping("/{type}")
    public Set<String> list(@PathVariable("type") String type){
        return chatHistoryRepository.list(type);
    }

    /**
     * 查询会话历史
     * @param type
     * @param chatId
     * @return
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type,@PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId, Integer.MAX_VALUE);
        if (messages==null){
            return List.of();
        }
        return messages.stream().map(MessageVO::new).toList();
    }
}
